class Attack < ActiveRecord::Base
  validates_presence_of :position1
  validates_presence_of :position2
  validates_presence_of :position3
  validates_presence_of :zeitpunkt

  has_many :armies, :dependent => :destroy
  has_many :defenders, :dependent => :destroy

  def to_s
    position1.to_s << ':' << position2.to_s << ':' << position3.to_s
  end

  def self.verarbeiten(input)
    positions = (/Du hast einen Angriff auf (\d+):(\d+):(\d+) geführt/).match(input)
    ergebnis = (/Du hast die Schlacht (\w+)/).match(input)
    @attack = Attack.create(:result => (ergebnis[1] == 'gewonnen'), :position1 => positions[1], :position2 => positions[2], :position3 => positions[3], :zeitpunkt => Time.now)

    # Angreifer
    angreifer = input.scan(/Angreifer(.*)Verteidiger/m).to_a[0].to_s
    angreifer_helden = angreifer.scan(/Held (.+?) von \w+/).to_a
    
    # currently unused because there is no defender model so far
    verteidiger = input.scan(/Verteidiger(.*)/m).to_a[0].to_s
    verteidiger_daten = verteidiger.scan(/(Wächter\sLevel:\s\d+|Palast\s.*?\svon\s.*|Held\s.*?von\s.*\sErfahrung:\s+\.*)/).to_a

    angreifer_helden.each do |held|
      unless Hero.find(:first, :conditions => ["name = ?", held]).nil?
        angreifer_data = angreifer.scan(/Held #{held} von (.*?Geplündert.*?$)/m).to_s
        # Rohstoffe
        essenzen = angreifer_data.scan(/Essenzen (.*?)\s/).to_a[0].to_s.delete(".")
        kristalle = angreifer_data.scan(/Kristallsplitter (.*?)\s/).to_a[0].to_s.delete(".")
        kraftsteine = angreifer_data.scan(/Kraftsteine (.*?)\s/).to_a[0].to_s.delete(".")
        goldharz = angreifer_data.scan(/Goldharz (.*?)\s/).to_a[0].to_s.delete(".")
        
        @army = Army.create(:attack => @attack, :hero => Hero.find(:first, :conditions => ["name= ?", held]), :essenzen => essenzen.to_i, :kristalle => kristalle.to_i, :kraftsteine => kraftsteine.to_i, :goldharz => goldharz.to_i)
        wesen_data = angreifer_data.scan(/(\w+\s?\w*\s+\d+\s+\d+\s+\d+)/).to_a
        wesen_data.each do |wesen|
          unit = wesen.to_s.match(/(\w+|\w+\s\w+)\s+(\d+)\s+\d+\s+(\d+)/).to_a
          Troop.create(:troopable => @army, :unit => Unit.find(:first, :conditions => ["name = ?", unit[1]]), :menge => unit[2], :losses => unit[3])
        end
      end
    end

    verteidiger_daten.each do |waechter|
      @defender = Defender.create(:attack => @attack, :name => waechter.to_s.delete(":"))
      verteidiger_data = verteidiger.scan(/#{waechter}\s(.*)/m).to_s
      wesen_data = verteidiger_data.scan(/(\w+\s?\w*\s+\d+\s+\d+\s+\d+)/).to_a
      wesen_data.each do |wesen|
        unit = wesen.to_s.match(/(\w+|\w+\s\w+)\s+(\d+)\s+\d+\s+(\d+)/).to_a
        Troop.create(:troopable => @defender, :unit => Unit.find(:first, :conditions => ["name = ?", unit[1]]), :menge => unit[2], :losses => unit[3])
      end
    end
    
    @attack
  end

  def beute_essenzen
    sum = 0
    self.armies.each do |army|
      sum = sum + army.essenzen unless army.essenzen.nil?
    end
    sum
  end

  def beute_kristalle
    sum = 0
    self.armies.each do |army|
      sum = sum + army.kristalle unless army.kristalle.nil?
    end
    sum
  end

  def beute_kraftsteine
    sum = 0
    self.armies.each do |army|
      sum = sum + army.kraftsteine unless army.kraftsteine.nil?
    end
    sum
  end

  def beute_goldharz
    sum = 0
    self.armies.each do |army|
      sum = sum + army.goldharz unless army.goldharz.nil?
    end
    sum
  end

  def verluste_essenzen
    sum = 0
    self.armies.each do |army|
      army.troops.each do |troop|
        sum = sum + troop.losses * troop.unit.essenzen
      end
    end
    sum
  end

  def verluste_kristalle
    sum = 0
    self.armies.each do |army|
      army.troops.each do |troop|
        sum = sum + troop.losses * troop.unit.kristalle
      end
    end
    sum
  end

  def verluste_kraftsteine
    sum = 0
    self.armies.each do |army|
      army.troops.each do |troop|
        sum = sum + troop.losses * troop.unit.kraftsteine
      end
    end
    sum
  end

  def verluste_goldharz
    sum = 0
    self.armies.each do |army|
      army.troops.each do |troop|
        sum = sum + troop.losses * troop.unit.goldharz
      end
    end
    sum
  end

  def summe_punkte(armies)
    sum = 0
    armies.each do |army|
        sum = sum + army.sum_punkte
    end
    sum
  end

  def summe_angriff(armies)
    sum = 0
    armies.each do |army|
      sum = sum + army.sum_angriff
    end
    sum
  end

  def summe_verteidigung(armies)
    sum = 0
    armies.each do |army|
      sum = sum + army.sum_verteidigung
    end
    sum
  end

  def summe_leben(armies)
    sum = 0
    armies.each do |army|
      sum = sum + army.sum_leben
    end
    sum
  end

  def gewinn_essenzen
    beute_essenzen - verluste_essenzen
  end

  def gewinn_kristalle
    beute_kristalle - verluste_kristalle
  end

  def gewinn_kraftsteine
    beute_kraftsteine - verluste_kraftsteine
  end

  def gewinn_goldharz
    beute_goldharz - verluste_goldharz
  end


  def verteilung_essenzen(army)
    verdient = army.verluste_essenzen + (anteil(gewinn_essenzen, army) - (army.essenzen.nil? ? 0 : army.essenzen))
    verdient.to_i
  end

  def verteilung_kristalle(army)
    verdient = army.verluste_kristalle + (anteil(gewinn_kristalle, army) - (army.kristalle.nil? ? 0 : army.kristalle))
    verdient.to_i
  end

  def verteilung_kraftsteine(army)
    verdient = army.verluste_kraftsteine + (anteil(gewinn_kraftsteine, army) - (army.kraftsteine.nil? ? 0 : army.kraftsteine))
    verdient.to_i
  end

  def verteilung_goldharz(army)
    verdient = army.verluste_goldharz + (anteil(gewinn_goldharz, army) - (army.goldharz.nil? ? 0 : army.goldharz))
    verdient.to_i
  end

  def verhaeltnis_angriff
    verhaeltnis = 1.0 * summe_angriff(self.armies)/summe_verteidigung(self.defenders)
    verhaeltnis.round(2)
  end

  def verhaeltnis_leben
    verhaeltnis = 1.0 * summe_leben(self.armies)/summe_leben(self.defenders)
    verhaeltnis.round(2)
  end

  def verhaeltnis_punkte
    verhaeltnis = 1.0 * summe_punkte(self.armies)/summe_punkte(self.defenders)
    verhaeltnis.round(2)
  end

  protected

  def anteil(essenzen, army)
    essenzen * (1.0 * army.sum_punkte/summe_punkte(self.armies))
  end
end
